// Modules to control application life and create native browser window
const {app, BrowserWindow, Menu, dialog, ipcMain} = require('electron')
const fs = require('fs')
const path = require('path')

function createWindow () {
  // Create the browser window.
  // 禁用窗口的菜单.
  Menu.setApplicationMenu(null);
  const mainWindow = new BrowserWindow({
    width: 1300,
    height: 830,
    minWidth: 1300,
    minHeight: 830,
    webPreferences: {
      nodeIntegration: true,
      webSecurity: false,
      preload: path.join(__dirname, 'preload.js')
    },
    title:"CRM系统",
    titleBarStyle: 'titleBarStyle',
    backgroundColor: 'linear-gradient(0deg,rgba(124,105,240,1),rgba(220,156,232,1)) no-repeat 0 0',
    // show: false
    // skipTaskbar: false
  })

  // and load the index.html of the app.
  // mainWindow.loadURL(' http://localhost:9528/')
  mainWindow.loadURL(' https://www.summerclub.club')

  // 打开开发者工具
  // mainWindow.webContents.openDevTools()

  // 点击关闭事件
  mainWindow.on('close', (event) => {
    event.preventDefault();

    // 先阻止默认功能的调用，否则会关闭窗口
    dialog.showMessageBox(mainWindow, {
      type: 'warning',
      title: '关闭',
      message: '请问你确定关闭该应用？',
      buttons: ["最小化","确定","取消"],
    }).then( (index) => {
      //选择了取消直接返回
      if (index.response === 2 ) {
        return true;
      } else if (index.response === 1 ){
        app.exit();
        return true;
      } else if (index.response === 0 ) {
        mainWindow.minimize();
        return true;
      }
    });
    return true;
  });

}
// 接收来自标识为file_url的消息
ipcMain.on('file_url', function (event, param) {
  if (param.flag === 'del'){
    fs.unlink(param.url, function(err) {
      if (err) {
        return console.error(err);
      }
      console.log("文件删除成功！");
    });
  } else {
    fs.stat(param.url, function (err, stats) {
      event.sender.send('file', stats);
    })
  }

  // deleteFolderRecursive(url)
});
// electron会在初始化完成并且准备好创建浏览器窗口时调用这个方法
// 部分 API 在 ready 事件触发后才能使用。
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') app.quit()
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

deleteFolderRecursive = function(url) {
  var files = [];
  console.log(url)
  //判断给定的路径是否存在
  if( fs.existsSync(url) ) {
    //返回文件和子目录的数组
    files = fs.readdirSync(url);
    files.forEach(function(file,index){
      // var curPath = url + "/" + file;
      var curPath = path.join(url,file);
      //fs.statSync同步读取文件夹文件，如果是文件夹，在重复触发函数
      if(fs.statSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
        // 是文件delete file
      } else {
        fs.unlinkSync(curPath);
      }
    });
    //清除文件夹
    fs.rmdirSync(url);
  }else{
    console.log("给定的路径不存在，请给出正确的路径");
  }
};
